package main;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import form.WindowsMsg;

public class getLocation implements KeyListener, WindowListener {

	JFrame jf;
	int no = 0, type;
	Point p;
	dataType.Point point;
	WindowsMsg win;
	int mask;
	int time;

	public getLocation() {
		jf = new JFrame();
		jf.setBounds(0, 550, 350, 25);
		jf.addKeyListener(this);
		jf.addWindowListener(this);
		jf.setResizable(false);
	}

	public void getInstrall(WindowsMsg win, int type) {

		this.type = type;
		switch (type) {
		case 1:
			jf.setTitle("召唤兽头像→气血位置按h");
			break;
		case 2:
			jf.setTitle("人物面板→气血位置按h");
			break;
		case 3:
			jf.setTitle("人物头像→魔法位置按h");
			break;
		case 4:
			jf.setTitle("人物头像→体力位置按h");
			break;
		default:
			return;
		}
		this.win = win;
		no = 0;
		jf.setVisible(true);
		jf.requestFocusInWindow();
	}

	public void PressKey(int mouse, int time) {
		if (mouse == 1) {
			this.mask = MouseEvent.BUTTON1_MASK;
		} else if (mouse == 2) {
			this.mask = MouseEvent.BUTTON3_MASK;
		} else {
			return;
		}
		this.time = time;
		this.type = 5;
		jf.setTitle("鼠标移动到需要按键的地方按'h'键");
		jf.setVisible(true);
		jf.requestFocusInWindow();
	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyChar() == 'h' || e.getKeyChar() == 'H') {
			switch (type) {
			case 1:
				getLocate1();
				break;
			case 2:
				getLocate2();
				break;
			case 3:
				getLocate3();
				break;
			case 4:
				getLocate4();
				break;
			case 5:
				getLocate5();

			}
		} else {
			jf.setVisible(false);
		}
	}

	public void keyPressed(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		jf.setVisible(false);
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
	}

	private void getLocate4() {

		switch (no) {
		case 0:
			no++;
			p = MouseInfo.getPointerInfo().getLocation();
			point = new dataType.Point(p.x, p.y);
			win.setXBlood(point);
			jf.setTitle("人物面板→魔力位置按h");
			break;
		case 1:
			no++;
			p = MouseInfo.getPointerInfo().getLocation();
			point = new dataType.Point(p.x, p.y);
			win.setXMagic(point);
			jf.setTitle("人物面板→更改属性位置按h");
			break;
		case 2:
			p = MouseInfo.getPointerInfo().getLocation();
			point = new dataType.Point(p.x, p.y);
			win.setMain(point);
			alert("设置成功！");
			break;
		default:
			alert("未知错误！");
			break;
		}
	}

	private void getLocate3() {
		// TODO getLocate3
		p = MouseInfo.getPointerInfo().getLocation();
		point = new dataType.Point(p.x, p.y);
		win.setMagic(point);
		alert("设置成功！");
	}

	private void getLocate2() {
		// TODO getLocate2
		p = MouseInfo.getPointerInfo().getLocation();
		point = new dataType.Point(p.x, p.y);
		win.setBlood(point);
		alert("设置成功！");
	}

	private void getLocate1() {
		// TODO getLocate1
		p = MouseInfo.getPointerInfo().getLocation();
		point = new dataType.Point(p.x, p.y);
		win.setBBlood(point);
		alert("设置成功！");
	}

	private void getLocate5() {
		try {
			Robot rb = new Robot();
			for (int a = 0; a < time; a++) {
				rb.mousePress(mask);
				Thread.sleep(50);
				rb.mouseRelease(mask);
				Thread.sleep(50);
			}
			alert("按键结束：" + time + "次");
		} catch (Exception e) {
			alert("发生未知错误，请重试！\n若不行请以管理员身份运行！");
		}
	}
	
	private void alert(String msg){
		JOptionPane.showMessageDialog(null, msg);
		jf.setVisible(false);
	}

}
